package org.sample.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreType;
import io.swagger.annotations.ApiModelProperty;

@JsonIgnoreType
public class PageInfo {

    @ApiModelProperty("当前页码")
    private int page = 1;

    @ApiModelProperty("每页显示行数")
    private int size = 5;

    @ApiModelProperty("是否显示总页数")
    private boolean showTotalPages = true;

    //是否有下一页
    @JsonIgnore
    private boolean hasNext;

    //总行数
    @JsonIgnore
    private int total;

    public PageInfo() {
    }

    public PageInfo(int page) {
        if (page > 0)
            this.page = page;
    }

    public PageInfo(int page, int size) {
        this(page);
        if (size > 0)
            this.size = size;
    }

    public PageInfo(int page, int size, boolean showTotalPages) {
        this(page);
        if (size > 0)
            this.size = size;
        this.showTotalPages = showTotalPages;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        if (page > 0)
            this.page = page;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        if (size > 0)
            this.size = size;
    }

    public boolean isShowTotalPages() {
        return showTotalPages;
    }

    public void setShowTotalPages(boolean showTotalPages) {
        this.showTotalPages = showTotalPages;
    }

    public boolean hasNext() {
        return hasNext;
    }

    public void setHasNext(boolean hasNext) {
        this.hasNext = hasNext;
    }

    public int getTotal() {
        return total;
    }

    public void setTotal(int total) {
        this.total = total;
        int totalPages = getTotalPages();
        this.hasNext = page < totalPages;
        if (total != 0 && page > totalPages)
            page = totalPages;
    }

    /**
     * 获取总页数
     */
    @JsonIgnore
    public int getTotalPages() {
        return (total - 1) / size + 1;
    }

    /**
     * 获取当页偏移量
     */
    @JsonIgnore
    public int getOffset() {
        return (page - 1) * size;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("{");
        sb.append("page:").append(page);
        sb.append(",totalPages:").append(getTotalPages());
        sb.append(",size:").append(size);
        sb.append(",offset:").append(getOffset());
        sb.append(",total:").append(total);
        sb.append("}");
        return sb.toString();
    }
}